﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PropertyManagement.Client.WinFormClient.Forms.Utilies;
using Zdm.Crm.Client.Entity.Entity;

namespace Zdm.Crm.Client.Winform
{
    public partial class LoginForm : XtraForm
    {

        public DevExpress.LookAndFeel.DefaultLookAndFeel DefaultLookAndFeel = new DevExpress.LookAndFeel.DefaultLookAndFeel();
        private string _filePath = AppDomain.CurrentDomain.BaseDirectory + "userinfo.dat";
        private string _imagePath = AppDomain.CurrentDomain.BaseDirectory + "image";
        public LoginForm()
        {
            InitializeComponent();
            FileInfo fi = new FileInfo(_filePath);
            if (fi.Exists)
            {

                try
                {
                    UserInfoEntity ui = FileUtil.LoadFromBinaryFile<UserInfoEntity>(_filePath);
                    user_te.Text = ui.UserId;
                    if (ui.SavePassword)
                    {
                        password_te.Text = ui.Password;
                        checkEdit1.Checked = ui.SavePassword;
                    }

                }
                catch
                {

                }
            }

            //随机数

            long tick = DateTime.Now.Ticks;
            Random ro = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
            int iDown = 0;


            DirectoryInfo dInfo = new DirectoryInfo(_imagePath);

            FileInfo[] fis = dInfo.GetFiles("*.jpg");
            if (fis.Length == 0)
                return;
            int iUp = fis.Length;
            FileInfo fileInfo = fis[ro.Next(iDown, iUp)];
            pictureEdit1.Image = Image.FromFile(fileInfo.FullName);
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Login();
        }




        private void Login()
        {
            if (string.IsNullOrEmpty(user_te.Text) || string.IsNullOrEmpty(password_te.Text))
            {
                XtraMessageBox.Show("用户名和密码不能为空！");
                return;
            }
            IDictionary<string, object> dic = new Dictionary<string, object> { { "UserId", user_te.Text }, { "Password", DataUtil.ToBase64(password_te.Text) } };
            IService service = ServiceFactory.GetService();
            var list = service.GetUserInfo(dic);
            if (list.Count == 0)
            {
                XtraMessageBox.Show("用户名或密码错误！");
                return;
            }
            UserInfoEntity ui = list[0];
            ui.Password = password_te.Text;
            ui.SavePassword = checkEdit1.Checked;
            ConfigCache.UserInfo = ui;
            FileUtil.SaveToBinaryFile(ui, _filePath);

            Thread thrT = new Thread(() => Application.Run(new XtraMainForm()));
            thrT.ApartmentState = ApartmentState.STA;
            thrT.Start();
            while (thrT.ThreadState != ThreadState.Running)
            {
            }
            Close();

        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Localization.GridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraGridLocalizationCHS();
            //DevExpress.Utils.Localization.AccLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressUtilsLocalizationCHS();
            DevExpress.XtraBars.Localization.BarLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraBarsLocalizationCHS();

            DevExpress.XtraEditors.Controls.Localizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraEditorsLocalizationCHS();
            DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraPrintingLocalizationCHS();

            DevExpress.FOSOYO.LookAndFeelSettings.Load("skin.ini");
            //DevMenu
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Dispose(true);
        }
    }
}